<template>
  <div class="custom-conditional-logic">
    <el-button type="text" @click="handleBackspace">
      <i class="el-icon-back"></i> 回退
    </el-button>
    <el-button type="text" @click="handleReset">
      <i class="el-icon-delete"></i> 清空
    </el-button>
    <div class="custom-conditional-logic__value">
      {{ result }}
    </div>
    <!-- <el-input type="textarea" autosize :value="JSON.stringify(value, null, 4)" /> -->
    <div class="custom-conditional-logic__control">
      <div>
        <el-button v-for="(item) in logic" :key="item.value" type='success' size="mini"
          @click="handleAddItem(item)">{{  item.label  }}</el-button>
        <el-button v-for="(item) in parenthesis" :key="item.value" type='danger' size="mini"
          @click="handleAddItem(item)">{{  item.label  }}</el-button>
        <el-button v-for="(item) in other" :key="item.value" type='danger' size="mini"
          @click="handleAddItem(item)">{{  item.label  }}</el-button>
      </div>
      <div class="group">
        <el-button v-for="(item, index) in groupList" :key="index" type='primary' size="mini"
          @click="handleAddItem(item)">{{  item.label  }}</el-button>
      </div>
    </div>
  </div>
</template>

<script>
const PARENTHESIS = [
  {
    label: '(',
    value: '('
  },
  {
    label: ')',
    value: ')'
  }
]

const LOGIC = [
  {
    label: '或',
    value: "||"
  },
  {
    label: '且',
    value: "&&"
  }
]

const OTHER = [
  {
    label: '!',
    value: "!"
  }
]


export default {
  name: 'CustomConditionalLogic',
  props: {
    value: {
      type: Array,
      default: () => []
    },
    pconditions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      parenthesis: PARENTHESIS,
      logic: LOGIC,
      other: OTHER
    }
  },
  computed: {
    groupList() {
      return this.pconditions.map((item, index) => {
        return {
          label: `条件组${index + 1}`,
          value: item.id,
          type: 'group'
        }
      })
    },
    result() {
      return this.value.map(item => item.label).join('')
    }
  },
  methods: {
    handleAddItem(item) {
      const value = [...this.value, item]
      this.$emit('input', value)
    },
    handleReset() {
      this.$emit('input', [])
    },
    handleBackspace() {
      const value = [...this.value]
      value.pop()
      this.$emit('input', value)
    }
  }
}
</script>

<style lang="scss" scoped>
.custom-conditional-logic {
  padding: 10px;

  &__value {
    min-height: 100px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    padding: 10px 15px;
    color: #606266;
    font-size: 13px;
  }

  &__control {
    margin-top: 10px;

    .group {
      margin-top: 10px;
    }
  }
}
</style>
